package com.ZengQi.event.dao.impl;

import com.ZengQi.event.dao.AdminDao;
import com.ZengQi.event.entity.Admin;
import com.ZengQi.event.util.JDBCUtils;
import com.ZengQi.event.util.SearchUtils;
import com.ZengQi.event.util.UpdateUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class AdminDaoImpl implements AdminDao {

    @Override
    public Admin getAdmin(Admin conditionAdmin) {
        String sql="select admin_id adminId, login_pnum loginPnum,login_pwd loginPwd,admin_name adminName,admin_company adminCompany from admin where login_pnum=?";
        List<Admin> admins = SearchUtils.searchForList(Admin.class, sql, conditionAdmin.getLoginPnum());
        if(admins.size()==0){
            return null;
        }
        return admins.get(0);

    }
    @Override
    public boolean deleteAdmin(Admin admin) {
        String sql="delete from admin where admin_name=?";
        boolean update = UpdateUtils.update(sql, admin.getAdminName());
        return update;
    }

    @Override
    public boolean insertAdmin(Admin admin) {
        String sql="insert into admin values(null,?,?,?,?)";
        boolean update = UpdateUtils.update(sql, admin.getLoginPnum(), admin.getLoginPwd(), admin.getAdminName(), admin.getAdminCompany());
        return update;


    }

    @Override
    public boolean updateAdminByOldCompanyName(Admin conditionAdmin, String oldCompanyName) {
        String sql="update admin set admin_company=? where admin_company=?";
        boolean update = UpdateUtils.update(sql, conditionAdmin.getAdminCompany(), oldCompanyName);
        return update;
    }
}

